<template>
  <div>
    <el-upload
      list-type="picture-card"
      :limit="limitCount"
      action="#"
      :on-preview="preview"
      :on-change="changeFile"
      :on-remove="handleRemove"
      :file-list="fileList"
      :class="{disabled:limitCount=== 1&&fileList.length > 0}"
      :http-request="upload"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <!-- 进度条 -->
    <el-progress v-if="showPercent" :stroke-width="26" :percentage="percent" style="width:200px" />
    <el-dialog title="图片" :visible="showDialog" @close="showDialog = false">
      <img :src="imgUrl" style="width:100%" alt="">
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
var cos = new COS({
  SecretId: 'AKID6xYtxkGWWGjwsWtzylIYYHkwtZy6yZty', // 身份识别 ID
  SecretKey: 'uYwWquhDRP6XhbFciX6OQBgurWu2sRjk' // 身份密钥
})
export default {
  props: {
    limitCount: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      fileList: [], // 图片地址设置为数组
      showDialog: false, // 控制显示弹层
      imgUrl: '',
      percent: 0,
      showPercent: false,
      uid: ''

    }
  },
  methods: {
    preview(file) {
      // console.log(31, file)
      this.imgUrl = file.url
      this.showDialog = true
    },

    // 删除文件/图片
    handleRemove(file, newFileList) {
      // console.log(45, this.fileList)
      this.fileList = newFileList
      // console.log(47, this.fileList)
    },
    // 添加图片
    changeFile(file, newFileList) {
      this.fileList = newFileList
      console.log(63, this.fileList)
    },
    // 上传前的校验
    beforeUpload(file) {
      this.uid = file.uid
      console.log(70, this.uid)
      // console.log(56, file)
      // 校验大小
      const maxSize = 2 * 1024 * 1024
      if (file.size > maxSize) {
        this.$message.warning('图片大小不能超过2M')
        // 这个函数特固定写法，return  false
        return false
      }
      // 校验格式
      const whiteList = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp']
      if (!whiteList.includes(file.type)) {
        this.$message.warning('图片只支持image/jpeg image/png image/gif image/bmp')
        return false
      }
      this.showPercent = true
      return true
    },

    // 上传操作
    upload(params) {
      // console.log(74, params)
      if (params.file) {
        cos.putObject({
          Bucket: 'layman-1308563088', // 存储桶
          Region: 'ap-guangzhou', // 地域
          Key: params.file.name, // 文件名
          Body: params.file, // 要上传的文件对象
          // 进度条,也是需要箭头函数
          onProgress: (params) => {
            this.percent = params.percent * 100
            // console.log(74, params.percent)
          },
          StorageClass: 'STANDARD' // 上传的模式类型 直接默认 标准模式即可
          // 上传到腾讯云 =》 哪个存储桶 哪个地域的存储桶 文件  格式  名称 回调
          // 需要用箭头函数，不用箭头函数用function的话会报错，会改变this的指向
        }, (err, data) => {
          // data返回数据之后 应该如何处理
          console.log(108, err || data)
          if (!err && data.statusCode === 200) {
            this.fileList = this.fileList.filter(item => item.uid === this.uid)
            this.fileList[0].url = 'http://' + data.Location
            this.fileList[0].status = 'success'
            // const list = this.fileList.filter(item => item.uid === this.uid)
            // list.url = 'http://' + data.Location
            // list.status = 'success'

            setTimeout(() => {
              this.showPercent = false
              this.percent = 0
            }, 1000)
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
// 这种就不需要scss模式
//  .disabled .el-upload--picture-card {
//   display: none
// }

// 这是scss的穿透写法
.disabled{
  ::v-deep .el-upload--picture-card {
  display: none
}
}
</style>
